package com.zhiqingchun.web.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhiqingchun.dao.account.IUserDao;
import com.zhiqingchun.dao.account.UserResult;
import com.zhiqingchun.pojo.Usr;

@Controller
@RequestMapping("/register.html")
public class RegisterController {

	@RequestMapping(method = RequestMethod.GET)
	public String toRegister(ModelMap map) {
		return "login/register";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String register(ModelMap modelMap,HttpServletRequest request, HttpServletResponse response,
			String email, String usr, String pwd) {
		Usr usr2 = new Usr();
		usr2.setEmail(email);
		usr2.setUsr(usr);
		usr2.setPwd(pwd);
		UserResult userResult = userDao.add(usr2);
		if (userResult.isSuccess()) {
			request.getSession().setAttribute("user", userResult.getUsr());
			return "redirect:default.html";
		} else {
			return "redirect:error.html";
		}
		
	}
	
	@Autowired
	private IUserDao userDao;

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
}
